﻿using System;
using System.Data;
using CRS.Core.DataAccess;
using CRS.Core.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.Helper;

namespace CRS.Web.BaseData
{
    public partial class ConsumeInfo : VcPage
    {
        public string RoundId
        {
            get
            {
                var obj = SessionObjectHelper.GetSession(this, "RoundId");
                return obj == null? "": obj.ToString();
            }
            set
            {
                SessionObjectHelper.SetSession(this, "RoundId", value);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Expires = -1;
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                RoundId = Request.QueryString["RoundId"];
                lblRoomName.Text = Server.UrlDecode(Request.QueryString["RoomName"]);
            }
            BindCurrList();
            BindHisList();
        }

        public void BindCurrList()
        {
            DataTable dtResult = null;

            var acc = new CoreDataAccess();
            try
            {
                dtResult = acc.GetCostList(new Guid(RoundId), false);
            }
            finally
            {
                acc.CloseSession();
            }
            gvCurrList.DataSource = dtResult;
            gvCurrList.DataBind();
        }


        public void BindHisList()
        {

            DataTable dtResult = null;

            var acc = new CoreDataAccess();
            try
            {
                dtResult = acc.GetCostList(new Guid(RoundId), true);
            }
            finally
            {
                acc.CloseSession();
            }
            gvHisList.DataSource = dtResult;
            gvHisList.DataBind();
        }

        private bool CreateCostData(string goodsCode, Guid roundId)
        {
            if (String.IsNullOrEmpty(goodsCode) || roundId == Guid.Empty)
                return false;
            var acc = new CoreDataAccess();
            try
            {
                acc.BeginTransaction();
                acc.CreateCurrRoomCostData(goodsCode, VitCode.Castle.DataAccess.GenericContext.CurrUser.Contact.Id, 1, roundId);
                acc.Commit();
            }
            catch (Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
            return true;
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            var reVal = CreateCostData(txtGoodsCode.Text, new Guid(RoundId));
            txtGoodsCode.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void gvCurrList_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
        {
            
        }

        protected void gvCurrList_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            Alert.MessageBox(e.CommandName);
        }
    }
}
